﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_DetailProduct : System.Web.UI.Page
{
    DAOProduct dao_product = new DAOProduct();
    DAOCategory dao_category = new DAOCategory();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    protected void LoadData()
    {

        int ProId = Convert.ToInt32(Request.QueryString["proid"].ToString());
        String Command = Request.QueryString["command"].ToString();
        txtProId.Enabled = txtStartingDate.Enabled = false;
        if (Command.Equals("detail"))
        {
            txtProName.Enabled = txtCategory.Enabled = txtPrice.Enabled = txtQuantity.Enabled = txtContent.Enabled =chbxIsSale.Enabled = false;
            btnEdit.Visible = ProImage.Visible= true;
            calStartingDate.Visible = btnUpdate.Visible = btnCancel.Visible = btnReset.Visible = ddlCategory.Visible =UploadFile.Visible = false;
        }
        else
        {
            txtCategory.Visible = btnEdit.Visible = ProImage.Visible = false;
            ddlCategory.Visible = calStartingDate.Visible = btnUpdate.Visible = btnCancel.Visible = btnReset.Visible = UploadFile.Visible = true;
        }

        Product p = dao_product.GetProductById(ProId);
        
        txtProId.Text = p.ProId.ToString();
        txtProName.Text = p.ProName;
        txtCategory.Text = p.Category.CateName;
        txtPrice.Text = p.Price.ToString();
        txtQuantity.Text = p.Quantity.ToString();
        chbxIsSale.Checked = p.IsSale;
        txtStartingDate.Text = p.StartingDate.ToString("dd - MM - yyyy"); ;
        ProImage.ImageUrl = "~/images/" +p.Image;
        txtNameImage.Text = p.Image;
        

        ddlCategory.DataSource = dao_category.GetAllCategory();
        ddlCategory.DataTextField = "CateName";
        ddlCategory.DataValueField = "CateId";
        ddlCategory.DataBind();
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        txtProName.Enabled = txtCategory.Enabled = txtPrice.Enabled = txtQuantity.Enabled = txtContent.Enabled = chbxIsSale.Enabled = true;
        btnEdit.Visible = txtCategory.Visible = ProImage.Visible = false;
        calStartingDate.Visible = btnUpdate.Visible = btnCancel.Visible = btnReset.Visible = ddlCategory.Visible = UploadFile.Visible= true;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtProName.Enabled = txtCategory.Enabled = txtPrice.Enabled = txtQuantity.Enabled = txtContent.Enabled = chbxIsSale.Enabled = false;
        btnEdit.Visible = txtCategory.Visible = ProImage.Visible = true;
        calStartingDate.Visible = btnUpdate.Visible = btnCancel.Visible = btnReset.Visible = ddlCategory.Visible = UploadFile.Visible = false;
    }
    protected void calStartingDate_SelectionChanged(object sender, EventArgs e)
    {
        txtStartingDate.Text = calStartingDate.SelectedDate.ToString("dd - MM - yyyy");
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        String fileName = txtNameImage.Text;
        if (UploadFile.HasFile)
        {
            fileName = Path.GetFileName(UploadFile.PostedFile.FileName);
            UploadFile.PostedFile.SaveAs(Server.MapPath("~/images/") + fileName);
        }
        Category c = new Category { CateID = Convert.ToInt32(ddlCategory.SelectedValue) };

        Product product = new Product
        {
            ProId = Convert.ToInt32(txtProId.Text),
            Category = c,
            ProName = txtProName.Text,
            Image = fileName,
            Price = Convert.ToDouble(txtPrice.Text),
            Quantity = Convert.ToInt32(txtQuantity.Text),
            IsSale = chbxIsSale.Checked,
            StartingDate = Convert.ToDateTime(txtStartingDate.Text),
            Content = txtContent.Text
        };

        int kq = dao_product.UpdateProduct(product);
        if (kq >0)
        {
            Message.Text = "Update thanh cong";
        }
    }
    protected void UploadFile_DataBinding(object sender, EventArgs e)
    {
        Message.Text = Path.GetFileName(UploadFile.PostedFile.FileName);
    }
}